package com.doeis.core.db;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Page<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer page = 1;
	private Integer per = 10;
	private int totalPages;
	private int totalRecords;
	private List<T> results;

	public Page() {
	}

	public Page(Integer currentPage, Integer pageSize) {
		this.page = currentPage;
		this.per = pageSize;
	}

	public List<?> getResults() {
		if (this.results == null) {
			return new ArrayList<Object>();
		}
		return this.results;
	}

	public void setResults(List<T> results) {
		this.results = results;
	}

	public Integer getPage() {
		return this.page.intValue() < 1 ? 1 : this.page;
	}

	public void setPage(Integer currentPage) {
		this.page = currentPage;
	}

	public Integer getPer() {
		return this.per.intValue() < 1 ? 10 : this.per;
	}

	public void setPer(Integer pageSize) {
		this.per = pageSize;
	}

	public int getTotalPages() {
		return this.totalPages;
	}

	public int getTotalRecords() {
		return this.totalRecords;
	}

	public void setTotalRecords(int count) {
		this.totalRecords = count;
		this.totalPages = (this.totalRecords / getPer().intValue());
		if (this.totalRecords % getPer().intValue() != 0) {
			this.totalPages += 1;
		}

		if (this.totalPages != 0) {
			if (getPage().intValue() < 1)
				this.page = Integer.valueOf(1);
		} else
			this.page = Integer.valueOf(0);
	}
}